package socketing;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class V4ServerHttp {

    public static void main(String[] args) throws Exception {
        ExecutorService executorService = Executors.newFixedThreadPool(4);

        ServerSocket serverSocket = new ServerSocket(6666);
        while (true){

            Socket socket = serverSocket.accept();
            Tool.debug(socket.getPort()+"===>>"+socket.getLocalPort());
            executorService.submit(new SocketTask(socket));


        }
    }


}
class SocketTask implements Runnable{

    public SocketTask(Socket socket) {
        this.socket = socket;
        this.req = new MttpRequest();
        this.res = new MttpResponse();
    }
    private MttpRequest req;
    private MttpResponse res;
    private Socket socket;

    @Override
    public void run() {

        try {
            BufferedWriter writer = new BufferedWriter(
                    new OutputStreamWriter(
                            socket.getOutputStream(), StandardCharsets.UTF_8));
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(
                            socket.getInputStream(), StandardCharsets.UTF_8));
//            读取并装配req对象，此时请注意,content-length非常重要，它确定了请求的结束
            String s = reader.readLine();
            String[] split = s.split(":");
            req.setPath(split[0]);
            req.setBody(split[1]);

            res.setBufferedWriter(writer);
            Thread.sleep(2000);
//            接下来要走filter略过
            Boolean r =DebugInter.getInstance().prehandle(req,res);
            if (!r) req.setPath("wwwwwww");
            if (req.getPath().contains("some")){
//                controller1
                doGet(req,res);


            }else if (req.getPath().contains("any")){
//                controller2
                writer.write("200"+req.getBody()+"anyany");

            }else {
//                error controller
                Tool.debug("奇怪的请求　"+req);
                writer.write("只支持以下命令　some any...");
            }
//          继续走完filter
            DebugInter.getInstance().posthandle(req,res,"ooo");

            reader.close();
            writer.close();
            socket.close();


        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
//    servet  path = "/some"
    private static void doGet(MttpRequest req,MttpResponse res) throws IOException {
            res.getBufferedWriter().write("200:"+req.getBody().toUpperCase());
            res.getBufferedWriter().flush();

    }


    public Socket getSocket() {
        return socket;
    }

    public void setSocket(Socket socket) {
        this.socket = socket;
    }
    class MttpRequest{
        String body;
        String path;

        public String getBody() {
            return body;
        }

        public void setBody(String body) {
            this.body = body;
        }

        public String getPath() {
            return path;
        }

        public void setPath(String path) {
            this.path = path;
        }

        @Override
        public String toString() {
            return "MttpRequest{" +
                    "body='" + body + '\'' +
                    ", path='" + path + '\'' +
                    '}';
        }
    }
    class MttpResponse{
        String status;
        BufferedWriter bufferedWriter;

        public BufferedWriter getBufferedWriter() {
            return bufferedWriter;
        }

        public void setBufferedWriter(BufferedWriter bufferedWriter) {
            this.bufferedWriter = bufferedWriter;
        }

        public String getStatus() {
            return status;
        }

        public void setStatus(String status) {
            this.status = status;
        }

    }


}
